package com.exam.danangtranel.adapter;

import java.util.List;

import com.exam.danangtranel.Shopping.Tbshopping_activity;
import com.exam.danangtranel.filed.Tbshopping;
import com.example.danangtravel.Home;
import com.example.danangtravel.R;
import com.example.danangtravel.checknetwork;
import com.example.danangtravel.map.GetDistance;
import com.example.danangtravel.map.RoutePath;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class TbShoppingAdapter extends BaseAdapter {
	public Context context;
	public List<Tbshopping> list;
	public OnClickListener listenner;
	public ViewHolder viewHolder;
	public String kc;
	public TextView khoangcach;
	public checknetwork check;

	public TbShoppingAdapter(Context context, List<Tbshopping> list) {
		this.context = context;
		this.list = list;
	}

	public void setButtonListenner(OnClickListener listenner) {
		this.listenner = listenner;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return (Tbshopping) list.get(arg0);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.row_tbshopping, null);
			viewHolder = new ViewHolder();
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.list_name);
			viewHolder.add = (TextView) convertView.findViewById(R.id.list_add);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.name.setText(list.get(position).getName());
		viewHolder.add.setText(list.get(position).getAdress());
		convertView.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				check = new checknetwork(v.getContext());
				AlertDialog.Builder build = new AlertDialog.Builder(v
						.getContext());
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View layout = inflater.inflate(R.layout.dialog_shoping, null);
				build.setView(layout);
				build.show();
				if (check.isOnline()) {
					// Toast.makeText(v.getContext(),
					// "" + Home.latitude + "," + Home.longitude, 1000)
					// .show();
					ThreadTask thread = new ThreadTask();
					thread.execute(position);
				}
				khoangcach = (TextView) layout.findViewById(R.id.txtkhoangcach);
				// khoangcach.setText(kc);
				TextView name = (TextView) layout.findViewById(R.id.dia_name);
				name.setText(list.get(position).getAdress());// name
				TextView add = (TextView) layout.findViewById(R.id.dia_add);
				add.setText(list.get(position).getName()); // Adress
				ImageButton call = (ImageButton) layout
						.findViewById(R.id.btnCall);
				ImageButton map = (ImageButton) layout
						.findViewById(R.id.btnMap);
				call.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent i = new Intent(Intent.ACTION_DIAL, Uri
								.parse("tel: 05113"));
						v.getContext().startActivity(i);
					}
				});
				map.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (check.isOnline()) {
							Intent i = new Intent(v.getContext(),
									RoutePath.class);
							i.putExtra("latitude", list.get(position)
									.getLatitude());
							i.putExtra("longitude", list.get(position)
									.getLongitude());
							v.getContext().startActivity(i);
						} else
							Toast.makeText(v.getContext(),
									"Không có kết nối !", 1000).show();
					}
				});
			}
		});
		return convertView;

	}

	static class ViewHolder {
		TextView name, add;
	}

	class ThreadTask extends AsyncTask<Integer, Integer, Void> {

		@Override
		protected Void doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			GetDistance getdistance = new GetDistance();
			kc = getdistance.GetRoutDistane(Home.latitude, Home.longitude,
					Double.valueOf(list.get(params[0]).getLatitude()),
					Double.valueOf(list.get(params[0]).getLongitude()));
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			khoangcach.setText(kc);
		}
	}
}
